%% On the Optimal Design of Transfers and Income-Tax Progressivity
% Figures of tax functions estimates from CPS

clear; clc; close all;
addpath('../../../Matlab_Figure_Files');

%% Read inputs

T = readtable('cps_results_1.xlsx','sheet','tax_functions_figures');

%% HSV: Estimated in logs, shown in logs

log_y_pretax = table2array(T(:,4));
log_y_posttax = table2array(T(:,5));
log_y_posttax_hsv_logs = table2array(T(:,6));

%% HSV: Estimated in logs, shown in levels

y_pretax = table2array(T(:,2))/1000;
y_posttax = table2array(T(:,3))/1000;
y_posttax_hsv_logs = table2array(T(:,7))/1000;

%% HSV: Estimated in levels, shown in levels

y_posttax_hsv_levels = table2array(T(:,8))/1000;

%% HSV+T: Estimated in levels, shown in levels

y_posttax_hsvT_levels = table2array(T(:,9))/1000;

%% FGNV: estimated in levels, shown in levels

y_posttax_fgnv_comb_levels = table2array(T(:,10))/1000;
y_posttax_fgnv_sep_levels = table2array(T(:,11))/1000;

%% Plot with new functions

fig = figure;
subplot(1,2,1)
plot(y_pretax,y_posttax_hsv_logs-y_posttax,'o','MarkerSize',8,'Color',[0.8500, 0.3250, 0.0980],'MarkerFaceColor',[0.8500, 0.3250, 0.0980]); hold on
plot(y_pretax,y_posttax_hsvT_levels-y_posttax,'d','MarkerSize',6,'Color', [0.3010 0.7450 0.9330],'MarkerFaceColor', [0.3010 0.7450 0.9330]); hold on
plot(y_pretax,y_posttax_fgnv_sep_levels-y_posttax,'d','MarkerSize',5,'Color',[0.4660 0.6740 0.1880],'MarkerFaceColor',[0.4660 0.6740 0.1880]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Absolute errors (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-50 5])
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Log-linear (in logs)','Log-linear with transfer','New functions');
set(leg,'Interpreter','LaTex','Location','SouthWest','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
subplot(1,2,2)
plot(y_pretax,(y_posttax_hsv_logs-y_posttax)./y_pretax,'o','MarkerSize',8,'Color',[0.8500, 0.3250, 0.0980],'MarkerFaceColor',[0.8500, 0.3250, 0.0980]); hold on
plot(y_pretax,(y_posttax_hsvT_levels-y_posttax)./y_pretax,'d','MarkerSize',6,'Color', [0.3010 0.7450 0.9330],'MarkerFaceColor', [0.3010 0.7450 0.9330]); hold on
plot(y_pretax,(y_posttax_fgnv_sep_levels-y_posttax)./y_pretax,'d','MarkerSize',5,'Color',[0.4660 0.6740 0.1880],'MarkerFaceColor',[0.4660 0.6740 0.1880]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Relative errors','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-0.65 0.2])
xlim([min(y_pretax) max(y_pretax)])
set(gca,'TickLabelInterpreter','LaTex')

run graph_extended.m;
saveas(gcf,'data_errors_FGNV.pdf')


fig = figure;
subplot(1,2,1)
plot(y_pretax,y_posttax_hsv_logs-y_posttax,'o','MarkerSize',5,'Color',[0 0 0],'MarkerFaceColor',[0 0 0]); hold on
plot(y_pretax,y_posttax_hsvT_levels-y_posttax,'d','MarkerSize',5,'Color', [0.7 0.7 0.7],'MarkerFaceColor', [0.7 0.7 0.7]); hold on
plot(y_pretax,y_posttax_fgnv_sep_levels-y_posttax,'square','MarkerSize',5,'Color',[0.4 0.4 0.4],'MarkerFaceColor',[0.4 0.4 0.4]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Absolute errors (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-50 5])
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Log-linear (in logs)','Log-linear with transfer','New functions');
set(leg,'Interpreter','LaTex','Location','SouthWest','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
subplot(1,2,2)
plot(y_pretax,(y_posttax_hsv_logs-y_posttax)./y_pretax,'o','MarkerSize',5,'Color',[0 0 0],'MarkerFaceColor',[0 0 0]); hold on
plot(y_pretax,(y_posttax_hsvT_levels-y_posttax)./y_pretax,'d','MarkerSize',5,'Color', [0.7 0.7 0.7],'MarkerFaceColor', [0.7 0.7 0.7]); hold on
plot(y_pretax,(y_posttax_fgnv_sep_levels-y_posttax)./y_pretax,'square','MarkerSize',5,'Color',[0.4 0.4 0.4],'MarkerFaceColor',[0.4 0.4 0.4]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Relative errors','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-0.65 0.2])
xlim([min(y_pretax) max(y_pretax)])
set(gca,'TickLabelInterpreter','LaTex')

run graph_extended.m;
saveas(gcf,'data_errors_FGNV_bw.pdf')
saveas(gcf,'data_errors_FGNV_bw','epsc')


%% Plot with loglinear function

fig = figure;
subplot(1,2,1)
plot(y_pretax,y_posttax_hsv_logs-y_posttax,'o','MarkerSize',8,'Color',[0.8500, 0.3250, 0.0980],'MarkerFaceColor',[0.8500, 0.3250, 0.0980]); hold on
plot(y_pretax,y_posttax_hsv_levels-y_posttax,'o','MarkerSize',7,'Color', [0.9290 0.6940 0.1250],'MarkerFaceColor', [0.9290 0.6940 0.1250]); hold on
plot(y_pretax,y_posttax_hsvT_levels-y_posttax,'d','MarkerSize',6,'Color', [0.3010 0.7450 0.9330],'MarkerFaceColor', [0.3010 0.7450 0.9330]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Absolute errors (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-50 5])
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Log-linear (in logs)','Log-linear (in levels)','Log-linear with transfer');
set(leg,'Interpreter','LaTex','Location','SouthWest','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
subplot(1,2,2)
plot(y_pretax,(y_posttax_hsv_logs-y_posttax)./y_pretax,'o','MarkerSize',8,'Color',[0.8500, 0.3250, 0.0980],'MarkerFaceColor',[0.8500, 0.3250, 0.0980]); hold on
plot(y_pretax,(y_posttax_hsv_levels-y_posttax)./y_pretax,'o','MarkerSize',7,'Color', [0.9290 0.6940 0.1250],'MarkerFaceColor', [0.9290 0.6940 0.1250]); hold on
plot(y_pretax,(y_posttax_hsvT_levels-y_posttax)./y_pretax,'d','MarkerSize',6,'Color', [0.3010 0.7450 0.9330],'MarkerFaceColor', [0.3010 0.7450 0.9330]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Relative errors','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-0.65 0.2])
xlim([min(y_pretax) max(y_pretax)])
set(gca,'TickLabelInterpreter','LaTex')

run graph_extended.m;
saveas(gcf,'data_errors_HSVT.pdf')


fig = figure;
subplot(1,2,1)
plot(y_pretax,y_posttax_hsv_logs-y_posttax,'o','MarkerSize',5,'Color',[0 0 0],'MarkerFaceColor',[0 0 0]); hold on
plot(y_pretax,y_posttax_hsv_levels-y_posttax,'d','MarkerSize',5,'Color', [0.7 0.7 0.7],'MarkerFaceColor', [0.7 0.7 0.7]); hold on
plot(y_pretax,y_posttax_hsvT_levels-y_posttax,'square','MarkerSize',5,'Color', [0.4 0.4 0.4],'MarkerFaceColor', [0.4 0.4 0.4]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Absolute errors (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-50 5])
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Log-linear (in logs)','Log-linear (in levels)','Log-linear with transfer');
set(leg,'Interpreter','LaTex','Location','SouthWest','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
subplot(1,2,2)
plot(y_pretax,(y_posttax_hsv_logs-y_posttax)./y_pretax,'o','MarkerSize',5,'Color',[0 0 0],'MarkerFaceColor',[0 0 0]); hold on
plot(y_pretax,(y_posttax_hsv_levels-y_posttax)./y_pretax,'d','MarkerSize',5,'Color', [0.7 0.7 0.7],'MarkerFaceColor', [0.7 0.7 0.7]); hold on
plot(y_pretax,(y_posttax_hsvT_levels-y_posttax)./y_pretax,'square','MarkerSize',5,'Color', [0.4 0.4 0.4],'MarkerFaceColor', [0.4 0.4 0.4]); hold on
plot(y_pretax,0*y_pretax,':','Color',[0.4 0.4 0.4],'Linewidth',2)
ylabel('Relative errors','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
ylim([-0.65 0.2])
xlim([min(y_pretax) max(y_pretax)])
set(gca,'TickLabelInterpreter','LaTex')

run graph_extended.m;
saveas(gcf,'data_errors_HSVT_bw.pdf')
saveas(gcf,'data_errors_HSVT_bw','epsc')


%% Taxes and transfers

tax_fgnv = table2array(T(:,12))/1000;
tax = table2array(T(:,13))/1000;
transfer_fgnv = table2array(T(:,14))/1000;
transfer = table2array(T(:,15))/1000;
transfer_fgnv_joint = table2array(T(:,16))/1000;
tax_fgnv_joint = table2array(T(:,17))/1000;

yl = table2array(T(:,23))/1000;
tax_fgnv_yl = table2array(T(:,24))/1000;
tax_yl = table2array(T(:,25))/1000;

fig = figure;
subplot(1,2,1)
plot(yl,tax_yl,'o','Linewidth',2,'MarkerSize',8); hold on
plot(yl,tax_fgnv_yl,'-x','MarkerSize',8,'Color',[0.4660 0.6740 0.1880],'MarkerFaceColor',[0.4660 0.6740 0.1880],'Linewidth',2)
ylabel('Labor income tax (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax labor income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Data','Estimated tax');
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
subplot(1,2,2)
plot(y_pretax,transfer,'o','Linewidth',2,'MarkerSize',8); hold on
plot(y_pretax,transfer_fgnv,'-x','MarkerSize',8,'Color',[0.4660 0.6740 0.1880],'MarkerFaceColor',[0.4660 0.6740 0.1880],'Linewidth',2)
ylabel('Transfer (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Data','Estimated transfer');
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff

run graph_extended.m;
saveas(gcf,'9_fgnv_tT.pdf')

fig = figure;
subplot(1,2,1)
plot(yl,tax_yl,'o','Linewidth',2,'MarkerSize',8,'Color',[0 0 0]); hold on
plot(yl,tax_fgnv_yl,'-x','MarkerSize',8,'Color',[0.7 0.7 0.7],'MarkerFaceColor',[0.7 0.7 0.7],'Linewidth',2)
ylabel('Labor income tax (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax labor income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Data','Estimated tax');
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
subplot(1,2,2)
plot(y_pretax,transfer,'o','Linewidth',2,'MarkerSize',8,'Color',[0 0 0]); hold on
plot(y_pretax,transfer_fgnv,'-x','MarkerSize',8,'Color',[0.7 0.7 0.7],'MarkerFaceColor',[0.7 0.7 0.7],'Linewidth',2)
ylabel('Transfer (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-tax income (\$k)','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
xlim([min(y_pretax) max(y_pretax)])
leg = legend('Data','Estimated transfer');
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff

run graph_extended.m;
saveas(gcf,'9_fgnv_tT_bw.pdf')
saveas(gcf,'9_fgnv_tT_bw','epsc')

%% Separate Transfer Programs

snap = table2array(T(:,18))/1000;
housing = table2array(T(:,19))/1000;
welfare = table2array(T(:,20))/1000;
credits = table2array(T(:,21))/1000;
state_credits = table2array(T(:,22))/1000;

transfer_programs_stacked =[transfer,snap,housing,welfare,credits,state_credits];

fig = figure;
bar(transfer_programs_stacked(:,2:end),'stacked')
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
leg = legend('SNAP','Housing assistance','Welfare','Fed.\ credits','State credits');
set(leg,'Interpreter','LaTex','Location','NorthEast','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
ylabel('Transfers (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-$t\&T$ income $2.5\%$ bins','Interpreter','LaTex','Fontsize',12)
run graph_extended.m;
saveas(gcf,'10_transfer_split.pdf')

fig = figure;
ba = bar(transfer_programs_stacked(:,2:end),'stacked','FaceColor','flat');
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
leg = legend('SNAP','Housing assistance','Welfare','Fed.\ credits','State credits');
set(leg,'Interpreter','LaTex','Location','NorthEast','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
legend boxoff
ylabel('Transfers (\$k)','Interpreter','LaTex','Fontsize',12)
xlabel('Pre-$t\&T$ income $2.5\%$ bins','Interpreter','LaTex','Fontsize',12)
ba(1).CData = [0 0 0];
ba(5).CData = [0.85 0.85 0.85];
ba(4).CData = [0.70 0.70 0.70];
ba(3).CData = [0.55 0.55 0.55];
ba(2).CData = [0.40 0.40 0.40];
run graph_extended.m;
saveas(gcf,'10_transfer_split_bw.pdf')
saveas(gcf,'10_transfer_split_bw','epsc')



